package cn.luckdou.system.controller.global;

import java.util.ArrayList;
import java.util.List;

import cn.luckdou.system.model.global.GlobalYo;
import cn.luckdou.system.service.global.IGlobalService;
import cn.luckdou.system.model.global.GlobalVo;
import cn.luckdou.system.model.global.Global;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RequestParam;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import cn.luckdou.commons.base.BaseController;
import cn.luckdou.commons.result.PageInfo;
import org.apache.log4j.Logger;
import org.apache.commons.lang.StringUtils;
import cn.luckdou.commons.utils.JsonUtils;
import cn.luckdou.commons.utils.Des3Util;
/**
 * <p>Description:全球地区库管理 <／p>
 * Copyright: Copyright (c) 20017;
 * @author Dadou
 * @DateTime 2017-10-09 
 * @version 1.0
 **/
@Controller
@RequestMapping("/admin/global")
public class GlobalController extends BaseController {
	// log日志
	private static Logger logger = Logger.getLogger(GlobalController.class);
    
    @Autowired private IGlobalService globalService;
    
    /**
	 * <p>Description: 列表页面</p>
	 * @author Dadou   
	 * @DateTime 2017-10-09 
	 * @version 1.0
     * @return
     */
    @GetMapping("/manager")
    public String manager() {
        return "admin/system/global/globalList";
    }

	/**
	 * <p>Description: 列表分页</p>
	 * @author Dadou   
	 * @DateTime 2017-10-09 
	 * @version 1.0
     * @return
     */
    @PostMapping("/dataGrid")
    @ResponseBody
    public PageInfo dataGrid(Global global, Integer page, Integer rows, String sort, String order) {
        EntityWrapper<Global> wrapper = new EntityWrapper<Global>();
        wrapper.setEntity(global);
        Page<Global> pages = getPage(page, rows, sort, order);
        pages = globalService.selectPage(pages,wrapper);
        return pageToPageInfo(pages);
    }
    
    /**
	 * <p>Description: 添加页面</p>
	 * @author Dadou   
	 * @DateTime 2017-10-09 
	 * @version 1.0
     * @return
     */
    @GetMapping("/addPage")
    public String addPage() {
        return "admin/system/global/globalAdd";
    }
    
    /**
	 * 
	 * <p>Title: pageList</p>
	 * <p>Description: WAP调用分页列表</p>
	 * @author Dadou   
	 * @DateTime 2017-10-09 
	 * @param global  后台
	 * @return
	 */
    @PostMapping("/pageList")
    @ResponseBody
    public Page<Global> pageList(Global global,@RequestParam(defaultValue = "1") Integer page,@RequestParam(defaultValue = "10")  Integer rows,
			@RequestParam(defaultValue = "id") String sort,
			@RequestParam(defaultValue = "desc") String order) {
    	EntityWrapper<Global> wrapper = new EntityWrapper<Global>();
    	wrapper.setEntity(global);
    	Page<Global> pages = getPage(page, rows, sort, order);
    	pages = globalService.selectPage(pages,wrapper);
    	return pages;
    }
    
    /**
	 * 
	 * <p>Title: list</p>
	 * <p>Description: 后台调用列表</p>
	 * @author Dadou   
	 * @DateTime 2017-10-09 
	 * @param global  后台
	 * @return
	 */
	@RequestMapping("/list")
	@ResponseBody
	public Object list(Global global) {
		EntityWrapper<Global> wrapper = new EntityWrapper<Global>();
        wrapper.setEntity(global);
		List<Global> list = globalService.selectList(wrapper);
		return list;
	}
	
	/**
	 * 
	 * <p>Title: appList</p>
	 * <p>Description: API接口调用</p>
	 * @author Dadou   
	 * @DateTime 2017-10-09 
	 * @param global
	 * @param json  
	 * @return
	 */
	@RequestMapping("/app/list")
	@ResponseBody
	public Object appList(Global global, String json) {
		if (StringUtils.isNotBlank(json)) {
			try {
				json = Des3Util.decode(json);
				json=json.replaceAll("'", "\"");
				global=JsonUtils.jsonToPojo(json, Global.class);
				EntityWrapper<Global> wrapper = new EntityWrapper<Global>();
        		wrapper.setEntity(global);
				List<Global> list = globalService.selectList(wrapper);
				return success(list.isEmpty()?null:list);
			} catch (Exception e) {
				return error(e.getMessage());
			}
		}
		return error("参数不能为空");
		
	}
	


    /**
	 * <p>Description: 添加</p>
	 * @author Dadou   
	 * @DateTime 2017-10-09 
	 * @version 1.0
     * @param 
     * @return
     */
    @PostMapping("/add")
    @ResponseBody
    public Object add(Global global, String json) {
		if (StringUtils.isNotBlank(json)) {
			try {
				json = Des3Util.decode(json);
				json=json.replaceAll("'", "\"");
				global=JsonUtils.jsonToPojo(json, Global.class);
			} catch (Exception e) {
				return error(e.getMessage());
			}
		}
        boolean b = globalService.insert(global);
        if (b) {
            return renderSuccess("添加成功！");
        } else {
            return renderError("添加失败！");
        }
    }

    /**
	 * <p>Description: 删除</p>
	 * @author Dadou   
	 * @DateTime 2017-10-09 
	 * @version 1.0
     * @param id
     * @return
     */
    @PostMapping("/delete")
    @ResponseBody
    public Object delete(String id) {
        boolean b = globalService.deleteById(Long.parseLong(id));
        if (b) {
            return renderSuccess("删除成功！");
        } else {
            return renderError("删除失败！");
        }
    }
    
    /**
	 * <p>Description: 批量删除</p>
	 * @author Dadou   
	 * @DateTime 2017-10-09 
	 * @version 1.0
	 * @param ids
	 * @return
	 */
	@PostMapping("/deleteIds")
	@ResponseBody
	public Object deleteIds(String ids) {
		if (StringUtils.isNotBlank(ids)) {
			String[] split = ids.split(",");
			int num = 0;
			for (String id : split) {
				boolean b = globalService.deleteById(Long.parseLong(id));
				if (b) {
					num++;
				}
			}
			if (num > 0) {
				return renderSuccess("删除"+num+"条记录成功！");
			} else {
				return renderError("删除失败！");
			}
		}
		return renderError("参数错误");
	}

    /**
     * 编辑
     * @author Dadou   
	 * @DateTime 2017-10-09 
	 * @version 1.0
     * @param model
     * @param id
     * @return
     */
    @GetMapping("/editPage")
    public String editPage(Model model, String id) {
        Global global = globalService.selectById(id);
        model.addAttribute("global", global);
        return "admin/system/global/globalEdit";
    }

    /**
     * 编辑
     * @author Dadou   
	 * @DateTime 2017-10-09 
	 * @version 1.0
     * @param 
     * @return
     */
    @RequestMapping("/edit")
    @ResponseBody
    public Object edit(Global global, String json) {
		if (StringUtils.isNotBlank(json)) {
			try {
				json = Des3Util.decode(json);
				json=json.replaceAll("'", "\"");
				global=JsonUtils.jsonToPojo(json, Global.class);
			} catch (Exception e) {
				return error(e.getMessage());
			}
		}
        boolean b = globalService.updateById(global);
        if (b) {
            return renderSuccess("编辑成功！");
        } else {
            return renderError("编辑失败！");
        }
    }
	/**
	 * 查询所有数据（带子父级关系）
	 * @param global
	 * @return
	 *
	 */
	@RequestMapping("/findAll")
	@ResponseBody
	public List<GlobalYo>  findAllGlobalsAndChildren(Global global){
		List<GlobalVo> globalVos = new ArrayList<>();

		try {
			globalVos = globalService.findAllGlobalsAndChildren(global);
			globalService.caseToYo(globalVos);
		} catch (Exception e) {
			e.printStackTrace();
			throw  new RuntimeException();
		}
		return globalService.caseToYo(globalVos);

	}

}
